package com.artifact.mit.mypoint.fragment;

import static com.artifact.mit.mypoint.util.Variable.PREFS_NAME;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.artifact.mit.mypoint.R;
import com.artifact.mit.mypoint.activity.DashboardActivity;
import com.artifact.mit.mypoint.activity.LoginActivity;
import com.artifact.mit.mypoint.activity.MyPointBaseActivity;
import com.artifact.mit.mypoint.activity.OrderHistoryActivity;
import com.artifact.mit.mypoint.activity.PointConnectorActivity;
import com.artifact.mit.mypoint.activity.PointHistoryActivity;
import com.artifact.mit.mypoint.activity.ProfileActivity;
import com.artifact.mit.mypoint.activity.TransferPointActivity;
import com.artifact.mit.mypoint.adapter.MenuAdapter;
import com.artifact.mit.mypoint.model.MenuItem;

public class LeftMenuFragment extends ListFragment {
	private int current = 0;
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.left_nav_menu_contents, null);
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		MenuAdapter adapter = new MenuAdapter(getActivity());
		adapter.add(new MenuItem("Home", R.drawable.home));
		adapter.add(new MenuItem("Point Issuer Connector", R.drawable.point_issue_conector));
		adapter.add(new MenuItem("Transfer Point", R.drawable.transfer));
		adapter.add(new MenuItem("Catalogue", R.drawable.catalog, true));
		adapter.add(new MenuItem("Point History", R.drawable.point_history));
		adapter.add(new MenuItem("Order History", R.drawable.order_history));
		adapter.add(new MenuItem("Profile", R.drawable.profile));
		adapter.add(new MenuItem("Manage Account", R.drawable.manage_account));
		adapter.add(new MenuItem("Logout", R.drawable.logout));
		setListAdapter(adapter);
		getListView().setItemChecked(getArguments().getInt("menuCheckedIndex"), true);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		((MyPointBaseActivity) getActivity()).setProgressBar(View.GONE);
		switch (position) {
		case 0:
			if (current != 0) {
				FragmentTransaction f = this.getFragmentManager()
						.beginTransaction();
				DashboardActivity da = new DashboardActivity();
				f.replace(R.id.main_frame, da);
				f.commit();
				((MyPointBaseActivity) getActivity()).toggle();
				current = 0;
			}
			break;
		case 1:
			if (current != 1) {
				FragmentTransaction f = this.getFragmentManager()
						.beginTransaction();
				PointConnectorActivity pc = new PointConnectorActivity();
				f.replace(R.id.main_frame, pc);
				f.commit();
				((MyPointBaseActivity) getActivity()).toggle();
				current = 1;
			}
			break;
		case 2:
			if (current != 2) {
				FragmentTransaction f = this.getFragmentManager()
						.beginTransaction();
				TransferPointActivity tp = new TransferPointActivity();
				f.replace(R.id.main_frame, tp);
				f.commit();
				((MyPointBaseActivity) getActivity()).toggle();
				current = 2;
			}
			break;
		case 3:
			if (getArguments().getInt("menuCheckedIndex") != 3) {
				FragmentTransaction t = this.getFragmentManager()
						.beginTransaction();
				t.setCustomAnimations(R.anim.slide_in_right,
						R.anim.slide_out_left);
				NestedMenuFragment nestedMenuFragment = new NestedMenuFragment();
				Bundle bundle = new Bundle();
				bundle.putInt("level", 1);
				nestedMenuFragment.setArguments(bundle);
				t.replace(R.id.menu_frame, nestedMenuFragment);
				t.commit();
			}
			break;
		case 4:
			if (getArguments().getInt("menuCheckedIndex") != 4)
				startActivity(new Intent(getActivity(), PointHistoryActivity.class));
			break;
		case 5:
			if (getArguments().getInt("menuCheckedIndex") != 5)
				startActivity(new Intent(getActivity(), OrderHistoryActivity.class));
			break;
		case 6:
			if (getArguments().getInt("menuCheckedIndex") != 6)
				startActivity(new Intent(getActivity(), ProfileActivity.class));
			break;
		case 7:
			
			break;
		case 8:
			SharedPreferences settings = getActivity().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
			SharedPreferences.Editor editor = settings.edit();
			editor.remove("ctx");
			editor.remove("id");
			editor.commit();
			startActivity(new Intent(getActivity(), LoginActivity.class));
			getActivity().finish();
			break;
		}
	}
}
